package com.example.student.controller;

import com.example.student.entity.Student;
import com.example.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @GetMapping("/{id}")
    public Student getStudentById(@PathVariable Integer id){return studentService.getStudentById(id);}
    @GetMapping
    public List<Student> getAll(){return studentService.findAll();}
    @PostMapping
    public int addStudent(@RequestBody Student student){return studentService.save(student);}
    @PutMapping("/{id}")
    public int updateStudent(@PathVariable Integer id,@RequestBody Student student){
        student.setId(id);
        return studentService.save(student);
    }
    @DeleteMapping("/{id}")
    public int deleteStudent(@PathVariable Integer id){
        return studentService.delete(id);
    }
    @GetMapping("/list/{ids}")
    public List<Student> getStudentByIds(@PathVariable("ids") String ids){
        List<Long> id = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());
        return studentService.findByIds(id);
    }
}
